1 /*
2 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /* ****************************************************************
27 ******************************************************************
28 ******************************************************************
29 *** COPYRIGHT (c) Eastman Kodak Company, 1997
30 *** As an unpublished work pursuant to Title 17 of the United
31 *** States Code. All rights reserved.
32 ******************************************************************
33 ******************************************************************
34 ******************************************************************/
35
36 package java.awt.image;
37
38 import static sun.java2d.StateTrackable.State.*;
39
40 /**
41 * This class extends <CODE>DataBuffer</CODE> and stores data internally as shorts.
42 * <p>
43 * <a name="optimizations">
44 * Note that some implementations may function more efficiently
45 * if they can maintain control over how the data for an image is
46 * stored.
47 * For example, optimizations such as caching an image in video
48 * memory require that the implementation track all modifications
49 * to that data.
50 * Other implementations may operate better if they can store the
51 * data in locations other than a Java array.
52 * To maintain optimum compatibility with various optimizations
53 * it is best to avoid constructors and methods which expose the
54 * underlying storage as a Java array as noted below in the
55 * documentation for those methods.
56 * </a>
57 */
58 public final class DataBufferShort extends DataBuffer
59 {
60 /** The default data bank. */
61 short data[];
62
63 /** All data banks */
64 short bankdata[][];
65
66 /**
67 * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank and the
68 * specified size.
69 *
70 * @param size The size of the <CODE>DataBuffer</CODE>.
71 */
72 public DataBufferShort(int size) {
73 super(STABLE, TYPE_SHORT,size);
74 data = new short[size];
75 bankdata = new short[1][];
76 bankdata[0] = data;
77 }
78
79 /**
80 * Constructs a short-based <CODE>DataBuffer</CODE> with the specified number of
81 * banks all of which are the specified size.
82 *
83 * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
84 * @param numBanks The number of banks in the a<CODE>DataBuffer</CODE>.
85 */
86 public DataBufferShort(int size, int numBanks) {
87 super(STABLE, TYPE_SHORT,size,numBanks);
88 bankdata = new short[numBanks][];
89 for (int i= 0; i < numBanks; i++) {
90 bankdata[i] = new short[size];
91 }
92 data = bankdata[0];
93 }
94
95 /**
96 * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank using the
97 * specified array.
98 * Only the first <CODE>size</CODE> elements should be used by accessors of
99 * this <CODE>DataBuffer</CODE>. <CODE>dataArray</CODE> must be large enough to
100 * hold <CODE>size</CODE> elements.
101 * <p>
102 * Note that {@code DataBuffer} objects created by this constructor
103 * may be incompatible with <a href="#optimizations">performance
104 * optimizations</a> used by some implementations (such as caching
105 * an associated image in video memory).
106 *
107 * @param dataArray The short array for the <CODE>DataBuffer</CODE>.
108 * @param size The size of the <CODE>DataBuffer</CODE> bank.
109 */
110 public DataBufferShort(short dataArray[], int size) {
111 super(UNTRACKABLE, TYPE_SHORT, size);
112 data = dataArray;
113 bankdata = new short[1][];
114 bankdata[0] = data;
115 }
116
117 /**
118 * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank using the
119 * specified array, size, and offset. <CODE>dataArray</CODE> must have at least
120 * <CODE>offset</CODE> + <CODE>size</CODE> elements. Only elements <CODE>offset</CODE>
121 * through <CODE>offset</CODE> + <CODE>size</CODE> - 1
122 * should be used by accessors of this <CODE>DataBuffer</CODE>.
123 * <p>
124 * Note that {@code DataBuffer} objects created by this constructor
125 * may be incompatible with <a href="#optimizations">performance
126 * optimizations</a> used by some implementations (such as caching
127 * an associated image in video memory).
128 *
129 * @param dataArray The short array for the <CODE>DataBuffer</CODE>.
130 * @param size The size of the <CODE>DataBuffer</CODE> bank.
131 * @param offset The offset into the <CODE>dataArray</CODE>.
132 */
133 public DataBufferShort(short dataArray[], int size, int offset) {
134 super(UNTRACKABLE, TYPE_SHORT, size, 1, offset);
135 data = dataArray;
136 bankdata = new short[1][];
137 bankdata[0] = data;
138 }
139
140 /**
141 * Constructs a short-based <CODE>DataBuffer</CODE> with the specified arrays.
142 * The number of banks will be equal to <CODE>dataArray.length</CODE>.
143 * Only the first <CODE>size</CODE> elements of each array should be used by
144 * accessors of this <CODE>DataBuffer</CODE>.
145 * <p>
146 * Note that {@code DataBuffer} objects created by this constructor
147 * may be incompatible with <a href="#optimizations">performance
148 * optimizations</a> used by some implementations (such as caching
149 * an associated image in video memory).
150 *
151 * @param dataArray The short arrays for the <CODE>DataBuffer</CODE>.
152 * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
153 */
154 public DataBufferShort(short dataArray[][], int size) {
155 super(UNTRACKABLE, TYPE_SHORT, size, dataArray.length);
156 bankdata = (short[][]) dataArray.clone();
157 data = bankdata[0];
158 }
159
160 /**
161 * Constructs a short-based <CODE>DataBuffer</CODE> with the specified arrays, size,
162 * and offsets.
163 * The number of banks is equal to <CODE>dataArray.length</CODE>. Each array must
164 * be at least as large as <CODE>size</CODE> + the corresponding offset. There must
165 * be an entry in the offset array for each <CODE>dataArray</CODE> entry. For each
166 * bank, only elements <CODE>offset</CODE> through
167 * <CODE>offset</CODE> + <CODE>size</CODE> - 1 should be
168 * used by accessors of this <CODE>DataBuffer</CODE>.
169 * <p>
170 * Note that {@code DataBuffer} objects created by this constructor
171 * may be incompatible with <a href="#optimizations">performance
172 * optimizations</a> used by some implementations (such as caching
173 * an associated image in video memory).
174 *
175 * @param dataArray The short arrays for the <CODE>DataBuffer</CODE>.
176 * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
177 * @param offsets The offsets into each array.
178 */
179 public DataBufferShort(short dataArray[][], int size, int offsets[]) {
180 super(UNTRACKABLE, TYPE_SHORT, size, dataArray.length, offsets);
181 bankdata = (short[][]) dataArray.clone();
182 data = bankdata[0];
183 }
184
185 /**
186 * Returns the default (first) byte data array.
187 * <p>
188 * Note that calling this method may cause this {@code DataBuffer}
189 * object to be incompatible with <a href="#optimizations">performance
190 * optimizations</a> used by some implementations (such as caching
191 * an associated image in video memory).
192 *
193 * @return The first short data array.
194 */
195 public short[] getData() {
196 theTrackable.setUntrackable();
197 return data;
198 }
199
200 /**
201 * Returns the data array for the specified bank.
202 * <p>
203 * Note that calling this method may cause this {@code DataBuffer}
204 * object to be incompatible with <a href="#optimizations">performance
205 * optimizations</a> used by some implementations (such as caching
206 * an associated image in video memory).
207 *
208 * @param bank The bank whose data array you want to get.
209 * @return The data array for the specified bank.
210 */
211 public short[] getData(int bank) {
212 theTrackable.setUntrackable();
213 return bankdata[bank];
214 }
215
216 /**
217 * Returns the data arrays for all banks.
218 * <p>
219 * Note that calling this method may cause this {@code DataBuffer}
220 * object to be incompatible with <a href="#optimizations">performance
221 * optimizations</a> used by some implementations (such as caching
222 * an associated image in video memory).
223 *
224 * @return All of the data arrays.
225 */
226 public short[][] getBankData() {
227 theTrackable.setUntrackable();
228 return (short[][]) bankdata.clone();
229 }
230
231 /**
232 * Returns the requested data array element from the first (default) bank.
233 *
234 * @param i The data array element you want to get.
235 * @return The requested data array element as an integer.
236 * @see #setElem(int, int)
237 * @see #setElem(int, int, int)
238 */
239 public int getElem(int i) {
240 return (int)(data[i+offset]);
241 }
242
243 /**
244 * Returns the requested data array element from the specified bank.
245 *
246 * @param bank The bank from which you want to get a data array element.
247 * @param i The data array element you want to get.
248 * @return The requested data array element as an integer.
249 * @see #setElem(int, int)
250 * @see #setElem(int, int, int)
251 */
252 public int getElem(int bank, int i) {
253 return (int)(bankdata[bank][i+offsets[bank]]);
254 }
255
256 /**
257 * Sets the requested data array element in the first (default) bank
258 * to the specified value.
259 *
260 * @param i The data array element you want to set.
261 * @param val The integer value to which you want to set the data array element.
262 * @see #getElem(int)
263 * @see #getElem(int, int)
264 */
265 public void setElem(int i, int val) {
266 data[i+offset] = (short)val;
267 theTrackable.markDirty();
268 }
269
270 /**
271 * Sets the requested data array element in the specified bank
272 * from the given integer.
273 * @param bank The bank in which you want to set the data array element.
274 * @param i The data array element you want to set.
275 * @param val The integer value to which you want to set the specified data array element.
276 * @see #getElem(int)
277 * @see #getElem(int, int)
278 */
279 public void setElem(int bank, int i, int val) {
280 bankdata[bank][i+offsets[bank]] = (short)val;
281 theTrackable.markDirty();
282 }
283 }